package com.atguigu.yygh.hosp.controller;

import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    //查询全部
    @ApiOperation(value = "查询所有医院设置")
    @GetMapping("getAll")
    public Result getAllHospitalSet() {
        List<HospitalSet> hospitalSetList = hospitalSetService.list();
        return Result.ok(hospitalSetList);
    }

    //删除
    @ApiOperation(value = "逻辑删除医院设置")
    @DeleteMapping("{id}")
    public Result deleteHospitalSet(@PathVariable Long id) {
        boolean flag = hospitalSetService.removeById(id);
        if (flag) {
            return Result.ok();
        }
        return Result.fail();
    }

    //带分页条件查询
    @PostMapping("getPage/{pageNum}/{pageSize}")
    public Result getPageHospitalSet(@PathVariable Long pageNum, @PathVariable Long pageSize,
                                     @RequestBody HospitalSetQueryVo hospitalSetQueryVo) {
        //创建分页对象
        Page<HospitalSet> page = new Page<>(pageNum, pageSize);

        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();

        //创建条件对象
        LambdaQueryWrapper<HospitalSet> queryWrapper = new LambdaQueryWrapper<>();

        if (!StringUtils.isEmpty(hosname)) {
            queryWrapper.like(HospitalSet::getHosname, hosname);
        }
        if (!StringUtils.isEmpty(hoscode)) {
            queryWrapper.eq(HospitalSet::getHoscode, hoscode);
        }

        Page<HospitalSet> hospitalSetPage = hospitalSetService.page(page, queryWrapper);

        return Result.ok(hospitalSetPage);
    }

    //新增
    @PostMapping("add")
    public Result addHospitalSet(@RequestBody HospitalSet hospitalSet) {
        // 设置状态 1 能使用  0 不能使用
        hospitalSet.setStatus(1);

        //签名密钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));

        boolean save = hospitalSetService.save(hospitalSet);

        if (save) {
            return Result.ok();
        }
        return Result.fail();
    }

    //根据id查询
    @GetMapping("get/{id}")
    public Result getHospSet(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return Result.ok(hospitalSet);
    }

    //修改
    @PostMapping("update")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet) {
        boolean flag = hospitalSetService.updateById(hospitalSet);
        if (flag) {
            return Result.ok();
        }
        return Result.fail();
    }

    //批量删除
    @DeleteMapping("batchDelete")
    public Result batchDeleteHospitalSet(@RequestBody List<Long> idList) {
        hospitalSetService.removeByIds(idList);
        return Result.ok();
    }

    //医院设置锁定
    @PutMapping("lock/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id, @PathVariable Integer status) {
        //
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        hospitalSet.setStatus(status);
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

}
